package lib

import (
	"github.com/golang-jwt/jwt/v5"
	"time"
)

type CustomerInfo struct {
	Username string `json:"username"`
}

type MyCustomClaims struct {
	CustomerInfo
	jwt.RegisteredClaims
}

func ParseToken(tokenString string, key string) (*MyCustomClaims, error) {
	token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
		return []byte(key), nil
	})
	if err != nil {
		return nil, err
	}
	return token.Claims.(*MyCustomClaims), err
}

func CreateToken(key string, data CustomerInfo) (string, error) {
	mySigningKey := []byte(key)
	claims := MyCustomClaims{
		CustomerInfo: data,
		RegisteredClaims: jwt.RegisteredClaims{
			Issuer:    "secret",
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(5 * time.Minute)),
			IssuedAt:  jwt.NewNumericDate(time.Now()),
			NotBefore: jwt.NewNumericDate(time.Now()),
		},
	}
	tokenPoint := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	token, err := tokenPoint.SignedString(mySigningKey)
	return token, err
}
